<a href="https://github.com/socketstream/socketstream/edit/master/src/docs/tutorials/en/server_side_testing.ngdoc" class="improve-docs"><i class="icon-edit"> </i>Improve this doc</a><h1><code ng:non-bindable=""></code>
<div><span class="hint"></span>
</div>
</h1>
<div><div class="testing-your-app-page"><h2 id="testing-your-app">Testing your app</h2>
<p>Note: This feature is brand new and still in the &#39;experimental&#39; stage. Please help us by reporting any problems so we can perfect server-side testing in future releases.</p>
<p>SocketStream allows you to test your app using Node&#39;s <code>assert</code> module, or any test framework of your choice. We recommend using <a href="http://gruntjs.com/getting-started">Grunt</a>, <a href="http://visionmedia.github.com/mocha">Mocha</a> with <a href="https://github.com/visionmedia/should.js">should.js</a>. This is the combination used in the example below.</p>
<h4 id="testing-your-app_getting-started">Getting Started</h4>
<p>First install <code>grunt-cli</code> globally:</p>
<pre><code>[sudo] npm install -g grunt-cli
</code></pre><p>Create a directory for your tests:</p>
<pre><code>mkdir test
</code></pre><p>For this example we&#39;re going to test the <code>ss.rpc(&#39;app.square&#39;)</code> function shown below:</p>
<pre class="prettyprint linenums">
// in/server/rpc/app.js
exports.actions = function(req, res, ss) {
  return {
    square: function(number) {
      res(number * number);
    }
  }
}
</pre>
<p>Create a new test file in the <code>/test</code> directory:</p>
<pre class="prettyprint linenums">
// in /test/app.test.js
var ss = require('socketstream').start();

describe('app.square', function() {

  it('should square a number', function(done) {
    ss.rpc('app.square', 4, function(params) {
      params.toString().should.equal('16');
      done();
    });
  });

});
</pre>
<p>Run all your tests in <code>/test</code> with:</p>
<pre><code>grunt test
</code></pre><p>And you&#39;ll see the following output:</p>
<pre><code>✔ 1 test complete (1ms)
</code></pre><p>A few things to note about RPC tests:</p>
<ul>
<li>All <code>ss.rpc()</code> commands return an array of params (e.g. <code>[16]</code> in the example above). As two <code>array</code>s cannot be directly compared in Javascript, it is necessary to convert the response to a <code>string</code> before calling <code>should.equal()</code></li>
<li><p>Create as many test files as you like. Subsequent calls to <code>var ss = require(&#39;socketstream&#39;).start();</code> will return the same server instance from memory</p>
</li>
<li><p>Tip: Rather than typing <code>grunt</code> each time, add the following lines to <code>package.json</code> so you can run your tests with <code>npm test</code>:
<pre class="prettyprint linenums">
    "scripts": {
      "test": "node_modules/grunt-cli/bin/grunt"
     }
</pre>
</li>
</ul>
<h4 id="testing-your-app_sessions">Sessions</h4>
<p>A new Session (with a unique ID) is automatically created for you the first time you <code>start()</code> SocketStream. This allows you to test <code>ss.rpc()</code> commands which use <code>req.session.userId</code>.</p>
<h4 id="testing-your-app_what-can-i-test">What can I test?</h4>
<p>Right now you can only test <code>ss.rpc()</code> commands. A major goal of SocketStream 0.4 is to allow any Request Responders to be tested in a similar way.</p>
<p>We are also considering implementing a mock Publish Transport, to allow you to test <code>ss.publish()</code> commands.</p>
</div></div>
